<?php

require_once 'Connection.php';

class ThongKe {

    public $conn;

    function __construct() {
        $con = new Connection();
        $this->conn = $con->configDB();
    }

    function getListThongKe() {
        $provinceID = (isset($_GET['provinceID'])) ? $_GET['provinceID'] : "";
        $num = (isset($_GET['num'])) ? "" . $_GET['num'] : "";
        $from = (isset($_GET['from'])) ? $_GET['from'] : "";
        $to = (isset($_GET['to'])) ? $_GET['to'] : "";

        $noNum = strlen($num);

        $result = array();
        $result_obj = array();
        mysqli_query($this->conn, 'SET NAMES utf8');
        if ($provinceID == "B") {
            $strQ = NULL;
            if ($noNum == 2) {
                $strQ = mysqli_query($this->conn, "CALL Statistics_2_B('$from','$to',$num)");
            } else if ($noNum == 3) {
                $strQ = mysqli_query($this->conn, "CALL Statistics_3_B('$from','$to',$num)");
            } else if ($noNum == 4) {
                $strQ = mysqli_query($this->conn, "CALL Statistics_4_B('$from','$to',$num)");
            } else if ($noNum == 5) {
                $strQ = mysqli_query($this->conn, "CALL Statistics_5_B('$from','$to',$num)");
            }
            while ($row = mysqli_fetch_array($strQ)) {
                if (substr($row['G7_1'], strlen($row['G7_1']) - $noNum, $noNum) == $num || substr($row['G7_2'], strlen($row['G7_2']) - $noNum, $noNum) == $num || substr($row['G7_3'], strlen($row['G7_3']) - $noNum, $noNum) == $num || substr($row['G7_4'], strlen($row['G7_4']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultBDate'];
                    $result_obj["Giai"] = "Giai 7: " . $row['G7_1'] . '&nbsp; - &nbsp;' . $row['G7_2'] . '&nbsp; - &nbsp;' . $row['G7_3'] . '&nbsp; - &nbsp;' . $row['G7_4'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G6_1'], strlen($row['G6_1']) - $noNum, $noNum) == $num || substr($row['G6_2'], strlen($row['G6_2']) - $noNum, $noNum) == $num || substr($row['G6_3'], strlen($row['G6_3']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultBDate'];
                    $result_obj["Giai"] = "Giai 6: " . $row['G6_1'] . '&nbsp; - &nbsp;' . $row['G7_2'] . '&nbsp; - &nbsp;' . $row['G6_3'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G5_1'], strlen($row['G5_1']) - $noNum, $noNum) == $num || substr($row['G5_2'], strlen($row['G5_2']) - $noNum, $noNum) == $num || substr($row['G5_3'], strlen($row['G5_3']) - $noNum, $noNum) == $num || substr($row['G5_4'], strlen($row['G5_4']) - $noNum, $noNum) == $num || substr($row['G5_5'], strlen($row['G5_5']) - $noNum, $noNum) == $num || substr($row['G5_6'], strlen($row['G5_6']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultBDate'];
                    $result_obj["Giai"] = "Giai 5: " . $row['G5_1'] . '&nbsp; - &nbsp;' . $row['G5_2'] . '&nbsp; - &nbsp;' . $row['G5_3'] . '&nbsp; - &nbsp;' . $row['G5_4'] . '&nbsp; - &nbsp;' . $row['G5_5'] . '&nbsp; - &nbsp;' . $row['G5_6'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G4_1'], strlen($row['G4_1']) - $noNum, $noNum) == $num || substr($row['G4_2'], strlen($row['G4_2']) - $noNum, $noNum) == $num || substr($row['G4_3'], strlen($row['G4_3']) - $noNum, $noNum) == $num || substr($row['G4_4'], strlen($row['G4_4']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultBDate'];
                    $result_obj["Giai"] = "Giai 4: " . $row['G4_1'] . '&nbsp; - &nbsp;' . $row['G4_2'] . '&nbsp; - &nbsp;' . $row['G4_3'] . '&nbsp; - &nbsp;' . $row['G4_4'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G3_1'], strlen($row['G3_1']) - $noNum, $noNum) == $num || substr($row['G3_2'], strlen($row['G3_2']) - $noNum, $noNum) == $num || substr($row['G3_3'], strlen($row['G3_3']) - $noNum, $noNum) == $num || substr($row['G3_4'], strlen($row['G3_4']) - $noNum, $noNum) == $num || substr($row['G3_5'], strlen($row['G3_5']) - $noNum, $noNum) == $num || substr($row['G3_6'], strlen($row['G3_6']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultBDate'];
                    $result_obj["Giai"] = "Giai 3: " . $row['G3_1'] . '&nbsp; - &nbsp;' . $row['G3_2'] . '&nbsp; - &nbsp;' . $row['G3_3'] . '&nbsp; - &nbsp;' . $row['G3_4'] . '&nbsp; - &nbsp;' . $row['G3_5'] . '&nbsp; - &nbsp;' . $row['G3_6'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G2_1'], strlen($row['G2_1']) - $noNum, $noNum) == $num || substr($row['G2_2'], strlen($row['G2_2']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultBDate'];
                    $result_obj["Giai"] = "Giai 2: " . $row['G2_1'] . '&nbsp; - &nbsp;' . $row['G2_2'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G1'], strlen($row['G1']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultBDate'];
                    $result_obj["Giai"] = "Giai 1: " . $row['G1'];
                    array_push($result, $result_obj);
                }
                if (substr($row['GDB'], strlen($row['GDB']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultBDate'];
                    $result_obj["Giai"] = "   Giai DB:   " . $row['GDB'];
                    array_push($result, $result_obj);
                }
            }
        } else if ($provinceID == "T") {
            $strQ = NULL;
            if ($noNum == 2) {
                $strQ = mysqli_query($this->conn, "CALL Statistics_2_T('$from','$to',$num)");
            } else if ($noNum == 3) {
                $strQ = mysqli_query($this->conn, "CALL Statistics_3_T('$from','$to',$num)");
            } else if ($noNum == 4) {
                $strQ = mysqli_query($this->conn, "CALL Statistics_4_T('$from','$to',$num)");
            } else if ($noNum == 5) {
                $strQ = mysqli_query($this->conn, "CALL Statistics_5_T('$from','$to',$num)");
            } else if ($noNum == 6) {
                $strQ = mysqli_query($this->conn, "CALL Statistics_6_T('$from','$to',$num)");
            }
            while ($row = mysqli_fetch_array($strQ)) {
                if (substr($row['G8'], strlen($row['G8']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultTDate'];
                    $result_obj["Giai"] = "Giai 8: " . $row['G8'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G7'], strlen($row['G7']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultTDate'];
                    $result_obj["Giai"] = "Giai 7: " . $row['G7'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G6_1'], strlen($row['G6_1']) - $noNum, $noNum) == $num || substr($row['G6_2'], strlen($row['G6_2']) - $noNum, $noNum) == $num || substr($row['G6_3'], strlen($row['G6_3']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultTDate'];
                    $result_obj["Giai"] = "Giai 6: " . $row['G6_1'] . '&nbsp; - &nbsp;' . $row['G6_2'] . '&nbsp; - &nbsp;' . $row['G6_3'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G5'], strlen($row['G5']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultTDate'];
                    $result_obj["Giai"] = "Giai 5: " . $row['G5'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G4_1'], strlen($row['G4_1']) - $noNum, $noNum) == $num || substr($row['G4_2'], strlen($row['G4_2']) - $noNum, $noNum) == $num || substr($row['G4_3'], strlen($row['G4_3']) - $noNum, $noNum) == $num || substr($row['G4_4'], strlen($row['G4_4']) - $noNum, $noNum) == $num || substr($row['G4_5'], strlen($row['G4_5']) - $noNum, $noNum) == $num || substr($row['G4_6'], strlen($row['G4_6']) - $noNum, $noNum) == $num || substr($row['G4_7'], strlen($row['G4_7']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultTDate'];
                    $result_obj["Giai"] = "Giai 4: " . $row['G4_1'] . '&nbsp; - &nbsp;' . $row['G4_2'] . '&nbsp; - &nbsp;' . $row['G4_3'] . '&nbsp; - &nbsp;' . $row['G4_4'] . '&nbsp; - &nbsp;' . $row['G4_5'] . '&nbsp; - &nbsp;' . $row['G4_6'] . '&nbsp; - &nbsp;' . $row['G4_7'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G3_1'], strlen($row['G3_1']) - $noNum, $noNum) == $num || substr($row['G3_2'], strlen($row['G3_2']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultTDate'];
                    $result_obj["Giai"] = "Giai 3: " . $row['G3_1'] . '&nbsp; - &nbsp;' . $row['G3_2'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G2'], strlen($row['G2']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultTDate'];
                    $result_obj["Giai"] = "Giai 2: " . $row['G2'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G1'], strlen($row['G1']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultTDate'];
                    $result_obj["Giai"] = "Giai 1: " . $row['G1'];
                    array_push($result, $result_obj);
                }
                if (substr($row['GDB'], strlen($row['GDB']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultTDate'];
                    $result_obj["Giai"] = "Giai DB: " . $row['GDB'];
                    array_push($result, $result_obj);
                }
            }
        } else if ($provinceID == "N") {
            $strQ = NULL;
            if ($noNum == 2) {
                $strQ = mysqli_query($this->conn, "CALL Statistics_2_N('$from','$to',$num)");
            } else if ($noNum == 3) {
                $strQ = mysqli_query($this->conn, "CALL Statistics_3_N('$from','$to',$num)");
            } else if ($noNum == 4) {
                $strQ = mysqli_query($this->conn, "CALL Statistics_4_N('$from','$to',$num)");
            } else if ($noNum == 5) {
                $strQ = mysqli_query($this->conn, "CALL Statistics_5_N('$from','$to',$num)");
            } else if ($noNum == 6) {
                $strQ = mysqli_query($this->conn, "CALL Statistics_6_N('$from','$to',$num)");
            }
            while ($row = mysqli_fetch_array($strQ)) {
                if (substr($row['G8'], strlen($row['G8']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultNDate'];
                    $result_obj["Giai"] = "Giai 8: " . $row['G8'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G7'], strlen($row['G7']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultNDate'];
                    $result_obj["Giai"] = "Giai 7: " . $row['G7'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G6_1'], strlen($row['G6_1']) - $noNum, $noNum) == $num || substr($row['G6_2'], strlen($row['G6_2']) - $noNum, $noNum) == $num || substr($row['G6_3'], strlen($row['G6_3']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultNDate'];
                    $result_obj["Giai"] = "Giai 6: " . $row['G6_1'] . '&nbsp; - &nbsp;' . $row['G6_2'] . '&nbsp; - &nbsp;' . $row['G6_3'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G5'], strlen($row['G5']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultNDate'];
                    $result_obj["Giai"] = "Giai 5: " . $row['G5'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G4_1'], strlen($row['G4_1']) - $noNum, $noNum) == $num || substr($row['G4_2'], strlen($row['G4_2']) - $noNum, $noNum) == $num || substr($row['G4_3'], strlen($row['G4_3']) - $noNum, $noNum) == $num || substr($row['G4_4'], strlen($row['G4_4']) - $noNum, $noNum) == $num || substr($row['G4_5'], strlen($row['G4_5']) - $noNum, $noNum) == $num || substr($row['G4_6'], strlen($row['G4_6']) - $noNum, $noNum) == $num || substr($row['G4_7'], strlen($row['G4_7']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultNDate'];
                    $result_obj["Giai"] = "Giai 4: " . $row['G4_1'] . '&nbsp; - &nbsp;' . $row['G4_2'] . '&nbsp; - &nbsp;' . $row['G4_3'] . '&nbsp; - &nbsp;' . $row['G4_4'] . '&nbsp; - &nbsp;' . $row['G4_5'] . '&nbsp; - &nbsp;' . $row['G4_6'] . '&nbsp; - &nbsp;' . $row['G4_7'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G3_1'], strlen($row['G3_1']) - $noNum, $noNum) == $num || substr($row['G3_2'], strlen($row['G3_2']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultNDate'];
                    $result_obj["Giai"] = "Giai 3: " . $row['G3_1'] . '&nbsp; - &nbsp;' . $row['G3_2'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G2'], strlen($row['G2']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultNDate'];
                    $result_obj["Giai"] = "Giai 2: " . $row['G2'];
                    array_push($result, $result_obj);
                }
                if (substr($row['G1'], strlen($row['G1']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultNDate'];
                    $result_obj["Giai"] = "Giai 1: " . $row['G1'];
                    array_push($result, $result_obj);
                }
                if (substr($row['GDB'], strlen($row['GDB']) - $noNum, $noNum) == $num) {
                    $result_obj["So"] = $num;
                    $result_obj["Tinh"] = $row['Name'];
                    $result_obj["Ngay"] = $row['ResultNDate'];
                    $result_obj["Giai"] = "Giai DB: " . $row['GDB'];
                    array_push($result, $result_obj);
                }
            }
        }
        return $result;
    }

}
